<?php
/**
 *
 *
 * 功能说明：系统管理。
 *
 **/

namespace Qwadmin\Controller;

class NumberController extends ComController
{
    public function cai()
    {
        if (IS_POST) {
            $norm  = $_POST['name'];
            $norm2  = $_POST['name2'];
            $yue = date('m');
            // echo $norm . $norm2;
            // exit;
            $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();

            // P($ulist);
            $uname = $ulist['user'];
            // echo $uname;
            // exit;
            //判断当前用户是否是评分人;
            $pr = M('ping')->where(['norm'=>['in',array($norm,$norm2)],'yue'=>$yue,'pname'=>$uname])->select();
            // $pf = M('ping')->where(['norm'=>['in',array($norm,$norm2)],'yue'=>$yue,'pname'=>$uname])->select();

            // P($pr);
            if (empty($pr)) {
                echo '您不是评分人';
                exit;
          
            }else{
                foreach ($pr as  $value) {
                    if (!empty($value['score'])) {
                        $this->ajaxReturn('请不要重复评分');
                    }
                }
                
            }
            
            $list['result'] = $_POST['result'];
            $list['standard'] = $_POST['standard'];
            $list['score'] = $_POST['score'];
            $list2['result'] = $_POST['result2'];
            $list2['standard'] = $_POST['standard2'];
            $list2['score'] = $_POST['score2'];
 


            // P($norm);
            // 到诊完成率分数添加
            if (M('ping')->where(['norm'=>$norm,'pname'=>$uname])->save($list) > 0) {
                //成本控制率添加
                if (M('ping')->where(['norm'=>$norm2,'pname'=>$uname])->save($list2) > 0) {
                    
                    //查询到诊完成率评分总人数----------------------------------------------------------------------------------------
                        // echo '111';
                        // exit;
                        $plist = M('ping')->where(['norm'=>$norm,'yue'=>$yue])->distinct(true)->field('pname')->select();
                        // P($plist);
                        //查询到诊完成率已评分人数
                        $plist2 = M('ping')->where(['norm'=>$norm,'yue'=>$yue,'score'=>['neq','null']])->distinct(true)->field('pname')->select();
                        
                        //↓ 获取考核信息的id
                        $pidlist = M('plan')->where(['norm'=>$norm,'yue'=>$yue])->distinct(true)->field('id')->select();
                        foreach ($pidlist as  $va) {
                            $pid .= "`pid`="."'".$va['id']."'"." or ";
                        }
                        $pid = rtrim($pid,'or ');
                        //↑ 获取考核信息的id END 
                        
                        $num = count($plist);
                        $num2 = count($plist2);
                        //判断到诊完成率评分人是否都评完;
                        if ($num == $num2) {
                            //计算到诊完成率衡量结果核算结果的平均数;
                            $result = M('ping')->where(['norm'=>$norm,'yue'=>$yue,'result'=>['neq','null']])->distinct(true)->field('result,pname')->select();
                            // P($result);

                            //计算到诊完成率标准分的平均数;
                            $standard = M('ping')->where(['norm'=>$norm,'yue'=>$yue,'standard'=>['neq','null']])->distinct(true)->field('standard,pname')->select();

                            //计算到诊完成率考核得分的平均数;
                            $score = M('ping')->where(['norm'=>$norm,'yue'=>$yue,'score'=>['neq','null']])->distinct(true)->field('score,pname')->select();
                             // P($score);
                            $rnum = 0;
                            foreach ($result as  $v) {
                                $rnum += $v['result'];
                            }  

                            $snum = 0;
                            foreach ($standard as  $v) {
                                $snum += $v['standard'];
                            } 
                           
                            $ssnum = 0;
                            foreach ($score as  $v) {
                                $ssnum += $v['score'];
                            } 
                            // echo $rnum . '/'.$snum .'/'. $ssnum;
                            // echo '<br>';
                            // dump($result);
                            // dump($standard);
                            // dump($score);
                            // echo '<hr>';
                            //获得到诊完成率衡量结果核算结果的平均分;
                            $ping['result'] = $rnum / $num;
                            //获得到诊完成率标准分的平均分;
                            $ping['standard'] = $snum / $num;
                            //获得到诊完成率考核得分的平均分;
                            $ping['score'] = $ssnum / $num;
                            // P($ping);
                            //讲到诊完成率平均分添加到plan表里
                            if (M('plan')->where(['norm'=>$norm,'yue'=>$yue])->save($ping) > 0) {
                                //讲到诊完成率平均分添加到people表里
                                M('people')->where($pid)->save($ping);
                            }
                        }

                    //以上完成到诊完成率的平均分总和 ---------------------------------------------------------------------------------------------



                    //查询成本控制率评分总人数----------------------------------------------------------------------------------------
                        $clist = M('ping')->where(['norm'=>$norm2,'yue'=>$yue])->distinct(true)->field('pname')->select();
                        // dump($plist);
                        // exit;
                        //查询成本控制率已评分人数
                        $clist2 = M('ping')->where(['norm'=>$norm2,'yue'=>$yue,'score'=>['neq','null']])->distinct(true)->field('pname')->select();
                        // P($plist2);
                         //↓ 获取考核信息的id
                        $cidlist = M('plan')->where(['norm'=>$norm2,'yue'=>$yue])->distinct(true)->field('id')->select();
                        foreach ($cidlist as  $va) {
                            $cid .= "`pid`="."'".$va['id']."'"." or ";
                        }
                        $cid = rtrim($cid,'or ');
                        // echo $cid;
                        // exit;
                        //↑ 获取考核信息的id END 
                        $cnum = count($clist);
                        $cnum2 = count($clist2);
                        // echo $cnum . $cnum2;
                        // exit;
                        //判断成本控制率评分人是否都评完;
                        if ($cnum == $cnum2) {
                           $dresult = M('ping')->where(['norm'=>$norm2,'yue'=>$yue,'result'=>['neq','null']])->distinct(true)->field('result,pname')->select();

                            //计算到诊完成率标准分的平均数;
                            $dstandard = M('ping')->where(['norm'=>$norm2,'yue'=>$yue,'standard'=>['neq','null']])->distinct(true)->field('standard,pname')->select();

                            //计算到诊完成率考核得分的平均数;
                            $dscore = M('ping')->where(['norm'=>$norm2,'yue'=>$yue,'score'=>['neq','null']])->distinct(true)->field('score,pname')->select();
                             // P($score);
                            $drnum = 0;
                            foreach ($dresult as  $v) {
                                $drnum += $v['result'];
                            }  

                            $dsnum = 0;
                            foreach ($dstandard as  $v) {
                                $dsnum += $v['standard'];
                            } 
                           
                            $dssnum = 0;
                            foreach ($dscore as  $v) {
                                $dssnum += $v['score'];
                            } 
                            //获得到诊完成率衡量结果核算结果的平均分;
                            $dping['result'] = $drnum / $cnum;
                            //获得到诊完成率标准分的平均分;
                            $dping['standard'] = $dsnum / $cnum;
                            //获得到诊完成率考核得分的平均分;
                            $dping['score'] = $dssnum / $cnum;
                        
                          

                            //讲成本控制率平均分添加到plan表里
                            if (M('plan')->where(['norm'=>$norm2,'yue'=>$yue])->save($dping) > 0) {
                                //讲成本控制率平均分添加到people表里
                                M('people')->where($cid)->save($dping);
                            }
                        }

                    //以上完成成本控制率的平均分总和 ---------------------------------------------------------------------------------------------
                        echo '1111';
                    
                }
                
            }else{
                echo '2222';
            }
        }else{
            $this->display();
        }
    	
    }






    //内部客户满意度
    public function nei()
    {
        
        //查询部门及用户考核信息;
        $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();
        $uname = $ulist['user'];
        $plist = M('ping')->where(['pname'=>$uname,'m_type'=>2])->distinct(true)->field('uname,yue,ment,score,pid')->select();
        // P($plist);
        // $list = M('zong')->select();
        $time = date('Y');
        $yue = date('m');
        $this->assign('list',$plist);
        $this->assign('time',$time);
        $this->assign('yue',$yue);
        $this->display();

    }

    //内部客户满意度评分操作
    public function xiang()
    {
        if (IS_POST) {
            // P(I());
            $yue = date('m');

            $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();
            $uname = $ulist['user'];
            $num = ($_POST['gfen'] + $_POST['gofen'] + $_POST['ggfen'] + $_POST['zfen'] + $_POST['zzfen']) / 5;
            if ($_POST['zong'] != $num) {
                $this->error('总分错误,请填选平均分!');
                exit;
            }
            $where = [
                'uname' => $_POST['uname'],
                'pid' => $_POST['pid'],
                'pname' => $uname,
                'yue'  => $yue
            ];
            $ping['result'] = $num;
            if (M('ping')->where($where)->save($ping) > 0) {
                //評分總人數
                $pzong = M('ping')->where(['pid'=>$_POST['pid'],'uname'=>$_POST['uname'],'yue'=>$yue])->distinct(true)->field('pname')->count();
                //已評人數
                $px = M('ping')->where(['pid'=>$_POST['pid'],'uname'=>$_POST['uname'],'result'=>['neq','null'],'yue'=>$yue])->distinct(true)->field('pname')->count();
                // P($px);
                if ($px == $pzong) {
                    $zong = M('ping')->where(['pid'=>$_POST['pid'],'uname'=>$_POST['uname'],'yue'=>$yue,'result'=>['neq','null']])->distinct(true)->field('result,pname')->select();
                    $rnum = 0;
                    foreach ($zong as $key => $v) {
                        $rnum += $v['result'];
                           
                    }

                    //获得到內部客戶滿意度評分人總和的平均分;
                    $ping['result'] = $rnum / $pzong;
                    M('people')->where(['uname'=>$_POST['uname'],'pid'=>$_POST['pid'],'yue'=>$yue])->save($ping);
                 

                }
                

                $this->success('評分成功!');
            }else{
                $this->error('評分失敗!');
            }
          

        }else{
            // dump(I());
            // exit;
            
            $ulist = M('zong')->where(['uname'=>$_GET['uname']])->select();
            $pid = $_GET['pid'];
            $yue = $_GET['yue'];
            // P($list);
            $time = date('Y');
            $this->assign('time',$time);
            $this->assign('pid',$pid);
            $this->assign('yue',$yue);
            $this->assign('ulist',$ulist);
            $this->display();
        }
    }



    //内部流程
    public function bu()
    {
        if (IS_POST) {
            // P(I());
            // $list = $_POST;
            // 查詢當前登入用戶信息
            $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();
            $uname = $ulist['user'];
            $list['result'] = $_POST['result'];
            $list['standard'] = $_POST['standard'];
            $list['score'] = $_POST['score'];
            
            // P($pzong);
            
            // P($res);
            if (M('ping')->where(['pid'=>$_POST['id'],'yue'=>$_POST['yue'],'pname'=>$uname,'uname'=>$_POST['uname']])->save($list) > 0) {
                    //評分總人數
                    $pzong = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname']])->distinct(true)->field('pname')->count();
                    //已評人數
                    $px = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'score'=>['neq','null']])->distinct(true)->field('pname')->count();
                    if ($px == $pzong) {
                        $zong = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'yue'=>$_POST['yue'],'result'=>['neq','null']])->distinct(true)->field('result,pname,standard,score')->select();
                        // P($result);
                        $rnum = 0;
                        $snum = 0;
                        $scnum = 0;
                        foreach ($zong as $key => $v) {
                            $rnum += $v['result'];
                            $snum += $v['standard'];
                            $scnum += $v['score'];
                           
                        }
                        // dump($zong);
                        // echo  $rnum . '/' .$snum. '/' . $scnum;
                        // exit;
                        //获得到诊完成率衡量结果核算结果的平均分;
                        $ping['result'] = $rnum / $pzong;
                        // //获得到诊完成率标准分的平均分;
                        $ping['standard'] = $snum / $pzong;
                        // //获得到诊完成率考核得分的平均分;
                        $ping['score'] = $scnum / $pzong;
                        // dump($ping);
                    
                        // echo $rnum;
                   
                        // exit;
                        M('people')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'yue'=>$_POST['yue']])->save($ping);

                    }
                    


                echo '1111';
            }else{
                echo '2222';
            }
        }else{
            //查询当前用户信息
            $uid = $_SESSION['uid'];
            $ulist = M('member')->where(['uid'=>$uid])->find();
            // P($ulist);
            $uname = $ulist['user'];
            //查询当前用户所要评分考核信息表;
            $list = M('ping')->where(['pname'=>$uname,'m_type'=>'3'])->distinct(true)->field('pid,uname,yue,target,norm,weight,value,rule,num,ment,result,standard,score')->order('uname desc')->select();

            // P($list);
            // foreach ($plist as  $va) {
            //     $pid .= "`pid`="."'".$va['pid']."'"." or ";
            // }
            // $pid = rtrim($pid,'or ');
            // // echo $pid;
           
            // //查询被评分人信息;
            // $list = M('people')->where($pid)->order('uname desc')->select();
            
            // $pplist = M('ping')->where(['pname'=>$uname,'m_type'=>'3'])->select();
            // P($pplist);
            if (empty($list)) {
                $empty = '0';
                $this->assign('empty',$empty);
                $this->display();
                exit;
            }
            // P($list);

            $time = date('Y');
            $empty = '1';
            $this->assign('time',$time);
            $this->assign('empty',$empty);
            $this->assign('list',$list);
            $this->display();
        }
    }








    //学习成长
    public function xue()
    {
        if (IS_POST) {
            // P(I());
            // $list = $_POST;
            // 查詢當前登入用戶信息
            $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();
            $uname = $ulist['user'];
            $list['result'] = $_POST['result'];
            $list['standard'] = $_POST['standard'];
            $list['score'] = $_POST['score'];
            
            // P($pzong);
            
            // P($res);
            if (M('ping')->where(['pid'=>$_POST['id'],'yue'=>$_POST['yue'],'pname'=>$uname,'uname'=>$_POST['uname'],'m_type'=>'4'])->save($list) > 0) {
                    //評分總人數
                    $pzong = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname']])->distinct(true)->field('pname')->count();
                    //已評人數
                    $px = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'score'=>['neq','null']])->distinct(true)->field('pname')->count();
                    if ($px == $pzong) {
                        $zong = M('ping')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'yue'=>$_POST['yue'],'result'=>['neq','null']])->distinct(true)->field('result,pname,standard,score')->select();
                        // P($result);
                        $rnum = 0;
                        $snum = 0;
                        $scnum = 0;
                        foreach ($zong as $key => $v) {
                            $rnum += $v['result'];
                            $snum += $v['standard'];
                            $scnum += $v['score'];
                           
                        }
                        // dump($zong);
                        // echo  $rnum . '/' .$snum. '/' . $scnum;
                        // exit;
                        //获得到诊完成率衡量结果核算结果的平均分;
                        $ping['result'] = $rnum / $pzong;
                        // //获得到诊完成率标准分的平均分;
                        $ping['standard'] = $snum / $pzong;
                        // //获得到诊完成率考核得分的平均分;
                        $ping['score'] = $scnum / $pzong;
                        // dump($ping);
                    
                        // echo $rnum;
                   
                        // exit;
                        M('people')->where(['pid'=>$_POST['id'],'uname'=>$_POST['uname'],'yue'=>$_POST['yue']])->save($ping);

                    }
                    


                echo '1111';
            }else{
                echo '2222';
            }
        }else{
            //查询当前用户信息
            $uid = $_SESSION['uid'];
            $ulist = M('member')->where(['uid'=>$uid])->find();
            // P($ulist);
            $uname = $ulist['user'];
            //查询当前用户所要评分考核信息表;
            $list = M('ping')->where(['pname'=>$uname,'m_type'=>'4'])->distinct(true)->field('pid,uname,yue,target,norm,weight,value,rule,num,ment,result,standard,score')->order('uname desc')->select();

            // P($list);
            // foreach ($plist as  $va) {
            //     $pid .= "`pid`="."'".$va['pid']."'"." or ";
            // }
            // $pid = rtrim($pid,'or ');
            // // echo $pid;
           
            // //查询被评分人信息;
            // $list = M('people')->where($pid)->order('uname desc')->select();
            
            // $pplist = M('ping')->where(['pname'=>$uname,'m_type'=>'3'])->select();
            // P($pplist);
            if (empty($list)) {
                $empty = '0';
                $this->assign('empty',$empty);
                $this->display();
                exit;
            }
            // P($list);

            $time = date('Y');
            $empty = '1';
            $this->assign('time',$time);
            $this->assign('empty',$empty);
            $this->assign('list',$list);
            $this->display();
        }
    }


















 }